import 'package:flutter/material.dart';
import '../../helper/cacheHelper.dart';
import '../../helper/channelHelper.dart';
import '../../helper/uidata.dart';
import '../../model/docsend.dart';
import '../../model/signitem.dart';
import '../../pages/homex/upFilePanel.dart';
import '../../widgets/inputWidget.dart';

import '../../model/user.dart';
import '../../widgets/userSelect.dart';
import '../../helper/Helper.dart';

class DocSendAddPage extends StatefulWidget {
  @override
  DocSendAddPageState createState() => new DocSendAddPageState();
}

class DocSendAddPageState extends State<DocSendAddPage> {
  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();

  final String now = Helper.DateTimeNowString();
  late SignItem sh, qf;
  List<SignItem> hq = <SignItem>[];
  String spaceID = Helper.GetGuid();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  upload(BuildContext context) async {
    String st = "";
    st = st + sh.UserId + ",";
    await SignItem.Add(sh);
    for (var a in hq) {
      await SignItem.Add(a);
      st = st + a.UserId + ",";
    }
    await SignItem.Add(qf);
    st = st + qf.UserId + ",";

    DocSend s = new DocSend(0, spaceID, c1.text, c2.text, 0, Helper.GetGuid(),
        Helper.DateTimeNowString(), UIData.CurrentUser!.Guid, st, "");
    await DocSend.Add(s);

    ChannelHelper.showToast("发布成功！");
    Navigator.of(context).pop();
  }

  getSH(User u1) {
    sh = new SignItem(
        0, spaceID, 1, "审核", u1.Guid, false, "", Helper.DateTimeNowString());
    setState(() {});
  }

  addHQ(User u1) {
    var a1 = new SignItem(
        0, spaceID, 2, "会签", u1.Guid, false, "", Helper.DateTimeNowString());

    setState(() {
      hq.add(a1);
    });
  }

  getQF(User u1) {
    qf = new SignItem(
        0, spaceID, 3, "签发", u1.Guid, false, "", Helper.DateTimeNowString());
    setState(() {});
  }

  Widget _getHQButton(SignItem r1) {
    if (r1 == null) return Container();
    var u1 = CacheHelper.GetUser(r1.UserId);
    return Container(
        color: Colors.white54,
        // width: 100,
        child: TextButton(
          child: Text(u1.Name),
          onPressed: () {
            setState(() {
              hq.remove(r1);
            });
          },
        ));
  }

  List<Widget> _getHQList() {
    var xx = <Widget>[];
    for (var p1 in hq) {
      xx.add(_getHQButton(p1));
    }
    return xx;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("发文审批"),
      ),
      body: DocSendAddBody(context),
    );
  }

  DocSendAddBody(BuildContext context) => SingleChildScrollView(
        child: DocSendAddFields(context),
      );

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list5 = <Widget>[];

    list3.add(InputWidget.getInput2("发文标题", c1));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInput2("发文详情", c2));

    list1.add(InputWidget.inCard2(list3));

    list2.add(ListTile(
      title: Text("选择审核"),
      trailing: UserSelectWidget(getSH, "发文审核"),
      subtitle: _getHQButton(sh),
    ));

    list2.add(InputWidget.getDivider());
    list2.add(ListTile(
        title: Text("添加会签"),
        subtitle: Container(
          child: Wrap(
            children: _getHQList(),
            spacing: 12.0,
            runSpacing: 2.0,
          ),
        ),
        trailing: UserSelectWidget(addHQ, "发文会签")));
    list2.add(ListTile(
        title: Text("选择签发"),
        subtitle: _getHQButton(qf),
        trailing: UserSelectWidget(getQF, "发文签发")));

    list1.add(InputWidget.inCard2(list2));

    list4.add(Container(
        child: UpFilePanel(
      "发文附件",
      spaceID,
    )));

    list5.add(ListTile(
      title: Center(
          child: Text(
        "确定提交",
        style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
      )),
      onTap: () {
        upload(context);
      },
    ));

    list1.add(InputWidget.inCard2(list4));
    list1.add(InputWidget.inCard2(list5));

    return list1;
  }

  DocSendAddFields(BuildContext context) => Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: getWidgetList(context),
        ),
      );
}
